/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */ 
package de.rockon.fuzzy.controller.operators.defuzzyfy;

import de.rockon.fuzzy.controller.model.FuzzyPoint;
import de.rockon.fuzzy.controller.model.FuzzySet;

/**
 * Defuzzyfizierungsalgorithmus welcher das letzte Maximum ermittelt
 */
public class LastMaximumDefuzzyfier extends AbstractDefuzzyfier {

	/**
	 * Konstruktor
	 */
	public LastMaximumDefuzzyfier(FuzzySet resultSet) {
		super(resultSet);
	}

	@Override
	public double defuzzyfy() {
		double maxY = -1;
		boolean lower = false;

		for (FuzzyPoint point : resultSet) {
			if (point.getY() < maxY) {
				lower = true;
			}
			if (point.getY() > maxY) {
				lower = false;
			}

			if (point.getY() >= maxY && !lower) {
				maxY = point.getY();
				resultX = point.getX();
			}
		}
		return resultX;
	}

	@Override
	public String getDescription() {
		return "Defuzzyfizierungsalgorithmus welcher das letzte Maximum ermittelt";
	}

	@Override
	public String toString() {
		return "LastMaximum";
	}
}